package com.danskin.wechat.web;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.model.LoginUser;
import com.danskin.wechat.service.WxMenuClickService;
import com.danskin.wechat.util.DateTools;
import com.danskin.wechat.util.ExcelUtil;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;
import com.danskin.wechat.vo.WxMenuClickDetailVo;
import com.danskin.wechat.vo.WxMenuClickVo;

import jxl.write.WritableWorkbook;

@Controller
@RequestMapping("menuAnalyze")
public class MenuAnalyzeController extends BaseController {

	@Autowired
	private WxMenuClickService wxMenuClickService;

	/**
	 * 跳转到微信原生卡领取统计页面
	 * 
	 * @return
	 * @author makunsmac
	 */
	@RequestMapping(value = "jumpMenuAnalyze", method = RequestMethod.GET)
	public String jumpMenuAnalyze() {
		return "reportStatistics/menu_analyze";
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "selectWxMenuClick", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object selectWxMenuClick() {
		WxMenuClickVo menuClick = new WxMenuClickVo();

		try {
			Map map = getRequestParameter(getRequest());
			LoginUser loginUser = getLoginUser();
			map.put("mpId", loginUser.getMpId());

			menuClick = wxMenuClickService.selectWxMenuClickVoList(map);
		} catch (Exception e) {
			LogUtil.error("======根据时间查询菜单点击统计异常", e);
			return new AccessResponse<WxMenuClickVo>("-1", "查询异常", null);
		}
		return new AccessResponse<WxMenuClickVo>("0", "查询异常", menuClick);
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "selectWxMenuClickDetailList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object selectWxMenuClickDetailList(Page page) {
		List<WxMenuClickDetailVo> menuClickDetailList = new ArrayList<WxMenuClickDetailVo>();

		try {
			Map map = getRequestParameter(getRequest());
			LoginUser loginUser = getLoginUser();
			map.put("mpId", loginUser.getMpId());
			page.setMap(map);

			menuClickDetailList = wxMenuClickService.listPageWxMenuClickDetailVoList(page);
		} catch (Exception e) {
			LogUtil.error("======根据时间查询菜单点击详情异常", e);
		}
		return pagelistToJSONForBootStrapTable(menuClickDetailList, page);
	}

	/**
	 * 根据查询条件导出菜单统计excel
	 * 
	 * @param response
	 * @author makunsmac
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportExcel", method = RequestMethod.GET)
	public void exportExcel(HttpServletResponse response) {
		OutputStream out = null;

		WritableWorkbook exportExcelWorkBook = null;
		try {
			Map map = getRequestParameter(getRequest());
			LoginUser loginUser = getLoginUser();
			map.put("mpId", loginUser.getMpId());

			LogUtil.info("======根据查询条件导出菜单统计excel参数：" + ToStringBuilder.reflectionToString(map));

			out = response.getOutputStream();

			// 查询符合条件的数据
			List<WxMenuClickDetailVo> scanList = wxMenuClickService.selectWxMenuClickDetailVoList(map);

			// 封装表头
			LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
			fieldMap = packageExcelHead(fieldMap);

			// 执行导出
			exportExcelWorkBook = ExcelUtil.listToExcel(scanList, fieldMap, "菜单统计", out);

			String fileName = "菜单统计-" + new SimpleDateFormat(DateTools.YYYYMMDDHHMMSS).format(new Date()) + ".xls";// 文件名
			response.setContentType("application/x-download");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1"));
			exportExcelWorkBook.write();
			exportExcelWorkBook.close();
		} catch (Exception e) {
			LogUtil.error("======");
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (Exception ex) {
				LogUtil.error("======导出菜单统计流关闭失败", ex);
			}
		}
	}

	/**
	 * 封装表头
	 * 
	 * @param fieldMap
	 * @return
	 * @author makunsmac
	 */
	private LinkedHashMap<String, String> packageExcelHead(LinkedHashMap<String, String> fieldMap) {
		fieldMap.put("parentMenuName", "菜单");
		fieldMap.put("menuName", "子菜单");
		fieldMap.put("clickCount", "菜单点击次数");
		fieldMap.put("userCount", "菜单点击人数");

		return fieldMap;
	}
}
